<?php

namespace App\Http\Controllers;

use App\Models\LoginLog;
use App\Models\User;
use App\Traits\ValidationTrait;
use Exception;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    use ValidationTrait;
    /**
     * 账号登录
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function account_login(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            //验证
            $validator = $this->verify($request, 'validation.account_login');
            if ($validator->fails()) {
                $messages = $validator->messages()->first();
                throw new Exception($messages);
            }
            $account  = $request->get('account');
            $password = $request->get('password');
            $password = md5($password . config('public.password_key'));
            if (isPhone($account)) {
                $user = User::where(['phone' => $account, 'password' => $password])
                    ->whereIn('type', [1, 3, 4])->first();
            } else {
                $user = User::where(['account' => $account, 'password' => $password])
                    ->whereIn('type', [1, 3, 4])->first();
            }
            if (!$user) {
                throw new Exception('账号或密码错误！');
            }
            $ip = $request->getClientIp();
            //登录历史
            LoginLog::create([
                'userID'   => $user->userID,
                'province' => '',
                'city'     => '',
                'area'     => '',
                'street'   => '',
                'property' => 1,
                'type'     => 3,
                'extend'   => 1,
                'device'   => '',
                'ip'       => $ip,
                'time'     => time(),
            ]);
            $data['user_id']  = $user->userID;
            $data['nickname'] = $user->nickname;
            $data['account']  = $user->account;
            $data['headimg']  = $user->headimg;
            $data['type']     = $user->type;
            session()->put('login_info', $data);

            $status = 1;
            $msg    = '登录成功！';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }

    public function quick_login(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            //验证
            $validator = $this->verify($request, 'validation.quick_login');
            if ($validator->fails()) {
                $messages = $validator->messages()->first();

                throw new Exception($messages);
            }

            $phone = $request->get('phone');
            $code  = $request->get('code');
            $user  = User::where(['phone' => $phone])->whereIn('type', [1, 3, 4])->first();

            if (!$user) {
                throw new Exception('用户不存在！');
            }

            /*$sms = Sms::where(['phone' => $phone, 'type' => 1])->first();
            //验证码过期
            if (time() > $sms->expired_at) {
                throw new Exception("验证码已过期！");
            }
            //验证码错误
            if ($sms->code != $code) {
                throw new Exception('验证码错误！');
            }*/

            //登录成功，刷新token
            //$user->update(['token' => getToken()]);

            $ip = $request->getClientIp();
            //登录历史
            LoginLog::create([
                'userID'   => $user->userID,
                'province' => '',
                'city'     => '',
                'area'     => '',
                'street'   => '',
                'property' => 1,
                'type'     => 3,
                'extend'   => 1,
                'device'   => '',
                'ip'       => $ip,
                'time'     => time(),
            ]);
            //存入session
            $data['userID']   = $user->userID;
            $data['nickname'] = $user->nickname;
            $data['account']  = $user->account;
            $data['headimg']  = $user->headimg;
            $data['type']     = $user->type;
            session()->put('login_info', $data);
            $status = 1;
            $msg    = '登录成功！';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
